/*   1:    */ package org.intellij.plugins.relaxNG.validation;
/*   2:    */ 
/*   3:    */

import com.intellij.ide.errorTreeView.NewErrorTreeViewPanel;
import com.intellij.ide.errorTreeView.NewErrorTreeViewPanel.ProcessController;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.command.CommandProcessor;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.wm.ToolWindowId;
import com.intellij.openapi.wm.ToolWindowManager;
import com.intellij.ui.content.*;
import com.intellij.ui.content.MessageView.SERVICE;
import com.intellij.util.ui.ErrorTreeView;
import gnu.trove.THashSet;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Set;

/*   4:    */
/*   5:    */
/*   6:    */
/*   7:    */
/*   8:    */
/*   9:    */
/*  10:    */
/*  11:    */
/*  12:    */
/*  13:    */
/*  14:    */
/*  15:    */
/*  16:    */
/*  17:    */
/*  18:    */
/*  19:    */
/*  20:    */
/*  21:    */
/*  22:    */
/*  23:    */
/*  24:    */
/*  25:    */
/*  26:    */
/*  27:    */
/*  28:    */
/*  29:    */
/*  30:    */
/*  31:    */
/*  32:    */
/*  33:    */ 
/*  34:    */ public class MessageViewHelper
/*  35:    */ {
/*  36: 48 */   private static final Logger a = Logger.getInstance("#org.intellij.plugins.relaxNG.validation.MessageViewHelper");
/*  37:    */   private final Project b;
/*  38: 52 */   private final Set<String> c = new THashSet();
/*  39:    */   private final String d;
/*  40:    */   private final Key<NewErrorTreeViewPanel> e;
/*  41:    */   private NewErrorTreeViewPanel f;
/*  42: 58 */   private NewErrorTreeViewPanel.ProcessController g = MyProcessController.INSTANCE;
/*  43:    */   
/*  44:    */   public MessageViewHelper(Project paramProject, String paramString, Key<NewErrorTreeViewPanel> paramKey)
/*  45:    */   {
/*  46: 61 */     this.b = paramProject;
/*  47: 62 */     this.d = paramString;
/*  48: 63 */     this.e = paramKey;
/*  49:    */   }
/*  50:    */   
/*  51:    */   public synchronized void setProcessController(NewErrorTreeViewPanel.ProcessController paramProcessController)
/*  52:    */   {
/*  53: 67 */     if (this.f == null) {
/*  54: 68 */       this.g = paramProcessController;
/*  55:    */     } else {
/*  56: 70 */       this.f.setProcessController(paramProcessController);
/*  57:    */     }
/*  58:    */   }
/*  59:    */   
/*  60:    */   public synchronized void openMessageView(Runnable paramRunnable)
/*  61:    */   {
/*  62: 75 */     assert (this.f == null);
/*  63: 76 */     this.f = new NewErrorTreeViewPanel(this.b, null, true, true, paramRunnable);
/*  64: 77 */     a();
/*  65:    */   }
/*  66:    */   
/*  67:    */   public synchronized void processError(final SAXParseException paramSAXParseException, final boolean paramBoolean)
/*  68:    */   {
/*  69: 81 */     if ((this.c.size() == 0) && (this.f == null))
/*  70:    */     {
/*  71: 82 */       this.f = new NewErrorTreeViewPanel(this.b, null, true, true, null);
/*  72: 83 */       this.f.setProcessController(this.g);
/*  73: 84 */       a();
/*  74:    */     }
/*  75: 86 */     String str1 = paramSAXParseException.getLineNumber() + "|" + paramSAXParseException.getColumnNumber() + "|" + paramSAXParseException.getSystemId() + "|" + paramSAXParseException.getLocalizedMessage();
/*  76: 87 */     if (!this.c.add(str1)) {
/*  77: 88 */       return;
/*  78:    */     }
/*  79: 91 */     VirtualFile localVirtualFile1 = null;
/*  80: 92 */     String str2 = paramSAXParseException.getSystemId();
/*  81: 93 */     if (str2 != null) {
/*  82:    */       try
/*  83:    */       {
/*  84: 95 */         localVirtualFile1 = VfsUtil.findFileByURL(new URL(str2));
/*  85:    */       }
/*  86:    */       catch (MalformedURLException localMalformedURLException)
/*  87:    */       {
/*  88: 97 */         a.warn("systemId = " + str2);
/*  89: 98 */         a.error(localMalformedURLException);
/*  90:    */       }
/*  91:    */     }
/*  92:102 */     final VirtualFile localVirtualFile2 = localVirtualFile1;
/*  93:103 */     ApplicationManager.getApplication().invokeLater(new Runnable()
/*  94:    */     {
/*  95:    */       public void run()
/*  96:    */       {
/*  97:106 */         MessageViewHelper.this.f.addMessage(paramBoolean ? 5 : 4, new String[] { paramSAXParseException.getLocalizedMessage() }, localVirtualFile2, paramSAXParseException.getLineNumber() - 1, paramSAXParseException.getColumnNumber() - 1, null);
/*  98:    */       }
/*  99:    */     });
/* 100:    */   }
/* 101:    */   
/* 102:    */   public void close()
/* 103:    */   {
/* 104:118 */     a(null);
/* 105:    */   }
/* 106:    */   
/* 107:    */   private void a(Content paramContent)
/* 108:    */   {
/* 109:122 */     MessageView localMessageView = MessageView.SERVICE.getInstance(this.b);
/* 110:124 */     for (Content localContent : localMessageView.getContentManager().getContents()) {
/* 111:125 */       if ((!localContent.isPinned()) && 
/* 112:126 */         (this.d.equals(localContent.getDisplayName())) && (localContent != paramContent))
/* 113:    */       {
/* 114:127 */         ErrorTreeView localErrorTreeView = (ErrorTreeView)localContent.getComponent();
/* 115:128 */         if ((localErrorTreeView != null) && 
/* 116:129 */           (localMessageView.getContentManager().removeContent(localContent, true))) {
/* 117:130 */           localContent.release();
/* 118:    */         }
/* 119:    */       }
/* 120:    */     }
/* 121:    */   }
/* 122:    */   
/* 123:    */   private void a()
/* 124:    */   {
/* 125:138 */     CommandProcessor localCommandProcessor = CommandProcessor.getInstance();
/* 126:139 */     localCommandProcessor.executeCommand(this.b, new Runnable()
/* 127:    */     {
/* 128:    */       public void run()
/* 129:    */       {
/* 130:141 */         MessageView localMessageView = MessageView.SERVICE.getInstance(MessageViewHelper.this.b);
/* 131:142 */         Content localContent = ContentFactory.SERVICE.getInstance().createContent(MessageViewHelper.this.f.getComponent(), MessageViewHelper.this.d, true);
/* 132:143 */         localContent.putUserData(MessageViewHelper.this.e, MessageViewHelper.this.f);
/* 133:144 */         localMessageView.getContentManager().addContent(localContent);
/* 134:145 */         localMessageView.getContentManager().setSelectedContent(localContent);
/* 135:146 */         localMessageView.getContentManager().addContentManagerListener(new MessageViewHelper.CloseListener(localContent, MessageViewHelper.this.d, MessageViewHelper.this.f));
/* 136:147 */         MessageViewHelper.this.a(localContent);
/* 137:148 */         localMessageView.getContentManager().addContentManagerListener(new MessageViewHelper.MyContentDisposer(localContent, localMessageView, MessageViewHelper.this.e));
/* 138:    */       }
/* 139:148 */     }, "Open Message View", null);
/* 140:    */     
/* 141:    */ 
/* 142:    */ 
/* 143:152 */     ToolWindowManager.getInstance(this.b).getToolWindow(ToolWindowId.MESSAGES_WINDOW).activate(null);
/* 144:    */   }
/* 145:    */   
/* 146:    */   private static class MyProcessController
/* 147:    */     implements NewErrorTreeViewPanel.ProcessController
/* 148:    */   {
/* 149:156 */     public static final MyProcessController INSTANCE = new MyProcessController();
/* 150:    */     
/* 151:    */     public void stopProcess() {}
/* 152:    */     
/* 153:    */     public boolean isProcessStopped()
/* 154:    */     {
/* 155:162 */       return true;
/* 156:    */     }
/* 157:    */   }
/* 158:    */   
/* 159:    */   private static class CloseListener
/* 160:    */     extends ContentManagerAdapter
/* 161:    */   {
/* 162:    */     private final String a;
/* 163:    */     private NewErrorTreeViewPanel b;
/* 164:    */     private Content c;
/* 165:    */     
/* 166:    */     public CloseListener(Content paramContent, String paramString, NewErrorTreeViewPanel paramNewErrorTreeViewPanel)
/* 167:    */     {
/* 168:173 */       this.c = paramContent;
/* 169:174 */       this.a = paramString;
/* 170:175 */       this.b = paramNewErrorTreeViewPanel;
/* 171:    */     }
/* 172:    */     
/* 173:    */     public void contentRemoved(ContentManagerEvent paramContentManagerEvent)
/* 174:    */     {
/* 175:179 */       if (paramContentManagerEvent.getContent() == this.c)
/* 176:    */       {
/* 177:180 */         if (this.b.canControlProcess()) {
/* 178:181 */           this.b.stopProcess();
/* 179:    */         }
/* 180:183 */         this.b = null;
/* 181:    */         
/* 182:185 */         this.c.getManager().removeContentManagerListener(this);
/* 183:186 */         this.c.release();
/* 184:187 */         this.c = null;
/* 185:    */       }
/* 186:    */     }
/* 187:    */     
/* 188:    */     public void contentRemoveQuery(ContentManagerEvent paramContentManagerEvent)
/* 189:    */     {
/* 190:192 */       if ((paramContentManagerEvent.getContent() == this.c) && 
/* 191:193 */         (this.b != null) && (this.b.canControlProcess()) && (!this.b.isProcessStopped()))
/* 192:    */       {
/* 193:194 */         int i = Messages.showYesNoDialog(this.a + " Running", this.a + " is still running. Close anyway?", Messages.getQuestionIcon());
/* 194:199 */         if (i != 0) {
/* 195:200 */           paramContentManagerEvent.consume();
/* 196:    */         }
/* 197:    */       }
/* 198:    */     }
/* 199:    */   }
/* 200:    */   
/* 201:    */   private static class MyContentDisposer
/* 202:    */     extends ContentManagerAdapter
/* 203:    */   {
/* 204:    */     private final Content a;
/* 205:    */     private final MessageView b;
/* 206:    */     private final Key<NewErrorTreeViewPanel> c;
/* 207:    */     
/* 208:    */     MyContentDisposer(Content paramContent, MessageView paramMessageView, Key<NewErrorTreeViewPanel> paramKey)
/* 209:    */     {
/* 210:213 */       this.a = paramContent;
/* 211:214 */       this.b = paramMessageView;
/* 212:215 */       this.c = paramKey;
/* 213:    */     }
/* 214:    */     
/* 215:    */     public void contentRemoved(ContentManagerEvent paramContentManagerEvent)
/* 216:    */     {
/* 217:219 */       Content localContent = paramContentManagerEvent.getContent();
/* 218:220 */       if (!localContent.equals(this.a)) {
/* 219:221 */         return;
/* 220:    */       }
/* 221:223 */       this.b.getContentManager().removeContentManagerListener(this);
/* 222:224 */       NewErrorTreeViewPanel localNewErrorTreeViewPanel = (NewErrorTreeViewPanel)localContent.getUserData(this.c);
/* 223:225 */       if (localNewErrorTreeViewPanel != null) {
/* 224:226 */         localNewErrorTreeViewPanel.dispose();
/* 225:    */       }
/* 226:228 */       localContent.putUserData(this.c, null);
/* 227:    */     }
/* 228:    */   }
/* 229:    */   
/* 230:    */   public class ErrorHandler
/* 231:    */     extends DefaultHandler
/* 232:    */   {
/* 233:    */     private boolean a;
/* 234:    */     
/* 235:    */     public ErrorHandler() {}
/* 236:    */     
/* 237:    */     public void warning(SAXParseException paramSAXParseException)
/* 238:    */       throws SAXException
/* 239:    */     {
/* 240:237 */       this.a = true;
/* 241:238 */       MessageViewHelper.this.processError(paramSAXParseException, true);
/* 242:    */     }
/* 243:    */     
/* 244:    */     public void error(SAXParseException paramSAXParseException)
/* 245:    */       throws SAXException
/* 246:    */     {
/* 247:243 */       this.a = true;
/* 248:244 */       MessageViewHelper.this.processError(paramSAXParseException, false);
/* 249:    */     }
/* 250:    */     
/* 251:    */     public void fatalError(SAXParseException paramSAXParseException)
/* 252:    */       throws SAXException
/* 253:    */     {
/* 254:249 */       this.a = true;
/* 255:250 */       MessageViewHelper.this.processError(paramSAXParseException, false);
/* 256:    */     }
/* 257:    */     
/* 258:    */     public boolean hadErrorOrWarning()
/* 259:    */     {
/* 260:254 */       return this.a;
/* 261:    */     }
/* 262:    */   }
/* 263:    */ }



/* Location:           E:\idea\

 * Qualified Name:     org.intellij.plugins.relaxNG.validation.MessageViewHelper

 * JD-Core Version:    0.7.0.1

 */